package ObjetoBr;

import java.io.File;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;

public class Leitor{

	private File f;
	private char 		   buffer[];
	private BufferedReader bufferReader;

	private final static int tamanhoBuffer = 4096;
	private int c_atual;


	public Leitor(String arquivo){

		c_atual = 0;
	
		f = new File(arquivo);

   		try{  
			bufferReader = new BufferedReader(new FileReader(f));  			
			buffer 		 = new char[4096];
			bufferReader.read(buffer, 0, 4096);
		}catch (IOException erro){  
			System.err.println(erro);  
			System.exit(1);  
		}  
	}

	/**
	 * Retorna um caracter do arquivo.
	 */
	public char proximoCaracter(){

		char c = '\0';
	
		try{
			if(c_atual > 4095){
				bufferReader.mark(4095);
				bufferReader.read(buffer, 0, 4096);				
				c_atual = 0;
			}
			c = (buffer[c_atual] != 0) ? buffer[c_atual] : '\0';
		}catch(IOException ioex){ System.out.println("Porra"); }

 		++c_atual;
		return c;
	}

	/**
	 * 
	 */
	public boolean temProximo(){
		if(buffer[c_atual] != 0)
			return true;			
		return false;
	}

	public void devolverCaracter(){
		--c_atual;
		try{
		if(c_atual < 0){
			bufferReader.reset();
			c_atual = 4095;			
		}}catch(IOException ioex){}
	}


	public static void main(String[] args){

		Leitor l = new Leitor("MeuCodigo.br");

// 		while(l.proximoCaracter() != '\0'){
// 		while(!l.seFim())

			l.devolverCaracter();
			System.out.println(l.proximoCaracter());
			System.out.println(l.proximoCaracter());			
			System.out.println(l.proximoCaracter());
/*
			char c;
			while(l.temProximo()){
				c = l.proximoCaracter();
				System.out.print(c);
			}*/
// 		}

	}

}